<template>
  <div class="app-container define-table-content">
    <table-grid :reqData="reqData" ref="tableGrid" :sceneType="sceneType" :sceneId="sceneId">
    </table-grid>
    <el-dialog custom-class="my-common-dialog" width="50%" :show-close="false" :close-on-click-modal="false"
      :visible.sync="showDetail">
      <tenant-edit v-if="showDetail" :id="form.Id" :title="showTitle" />
    </el-dialog>
    <el-dialog custom-class="my-common-dialog" width="80%" :show-close="false" :close-on-click-modal="false"
      :visible.sync="showPower">
      <assign-power v-if="showPower" :id="form.Id" :title="showTitle" />
    </el-dialog>
  </div>
</template>

<script>
  import {
    queryTenants,
    deleteTenants
  } from '@/api/users/tenants'
  import tableGrid from "@/views/master/Table"
  import tenantEdit from '@/views/system/tenants/edit'
  import assignPower from '@/views/system/tenants/assignPower'
  export default {
    name: 'sys-tenants',
    components: {
      tableGrid,
      tenantEdit,
      assignPower
    },
    props: {
      sceneId: {
        type: Number,
        default: 0
      },
      focusId: {
        type: Number,
        default: 0
      },
      sceneType: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        reqData: {
          loading: true,
          // 返回结果集
          tableData: [],
          operation: {},
          // 合计条数
          total: 0,
          // 查询参数
          queryParams: {
            queryText: undefined,
            pageSize: 20,
            orderBy: 'st.Id',
            sort: 'descending',
            queryData: '',
            sortData: '',
            groupData: '',
            sceneId: 0,
            sceneType: 0,
            focusId: 0
          },
          rightMenuData: [],
        },
        // 选择表名列表
        selectTableNames: [],
        // 搜索条显示
        searchToggle: true,
        showDetail: false,
        showTitle: '',
        form: {
          Id: 0
        },
        allRightMenuData: [],
        showPower: false
      }
    },
    created() {
      var that = this
      that.reqData.queryParams.sceneId = this.sceneId
      that.reqData.queryParams.focusId = this.focusId
      that.reqData.queryParams.sceneType = this.sceneType
      that.getPermission()
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.reqData.loading = true
        queryTenants(this.reqData.queryParams).then(res => {
          this.reqData.loading = false
          if (res.status) {
            this.reqData.tableData = res.outputData.DataSource
            this.reqData.total = res.outputData.TotalCount
          }
        })
      },
      getPermission() {
        this.reqData.operation = {
          add: this.$checkPermission('sys_tenant_add'),
          edit: this.$checkPermission('sys_tenant_edit'),
          delete: this.$checkPermission('sys_tenant_remove'),
          hide: true,
          query: true,
          sort: true,
          lineHeight: true,
          canAuth: this.$checkPermission('sys_tenant_auth'),
          more: true
        }
        this.reqData.rightMenuData = []
        if (this.reqData.operation.edit) {
          this.reqData.rightMenuData.push({
            id: 1,
            icon: '',
            label: '编辑'
          })
        }
        if (this.reqData.operation.delete) {
          this.reqData.rightMenuData.push({
            id: 2,
            icon: '',
            label: '删除'
          })
        }
        if (this.reqData.operation.canAuth) {
          this.reqData.rightMenuData.push({
            id: 3,
            icon: '',
            label: '授权'
          })
        }
        this.allRightMenuData = this.reqData.rightMenuData
      },
      doQuery(queryData) {
        this.reqData.queryParams.queryData = JSON.stringify(queryData);
        this.getList();
      },
      doSort(sortData) {
        this.reqData.queryParams.sortData = JSON.stringify(sortData);
        this.getList();
      },
      doAdd() {
        this.form.Id = 0;
        this.showTitle = '新增租户'
        this.showDetail = true
      },
      doEdit(Id) {
        this.form.Id = Id
        this.showTitle = '修改租户'
        this.showDetail = true
      },
      doRemove(Ids) {
        if (Ids.length === 0) {
          return
        }
        this.$confirm(
            '是否确认删除选中的 ' + Ids.length + ' 条数据?',
            '警告', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          )
          .then(() => {
            deleteTenants({
              Ids: JSON.stringify(Ids)
            }).then(res => {
              if (res.status) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
                this.getList()
              }
            })
          })
      },
      doCloseDetail(reload) {
        this.showDetail = false
        if (reload) {
          this.getList();
        }
      },
      setRightMenu(row) {
        var that = this
        that.reqData.rightMenuData = []
        that.allRightMenuData.forEach(function(item) {
          that.reqData.rightMenuData.push(item)
        })
      },
      doClickRightMenu(item, row) {
        if (item.id == 1) {
          this.doEdit(row.Id);
        }
        if (item.id == 2) {
          var Ids = [row.Id];
          this.doRemove(Ids);
        }
        if (item.id == 3) {
          this.form.Id = row.Id;
          this.showTitle = '模块分配'
          this.showPower = true;
        }
      },
      doClosePower() {
        this.showPower = false
      },
      doSearch(option) {
        if (option) {
          if (option.queryText) {
            this.reqData.queryParams.queryText = option.queryText;
          }
          if (option.queryData) {
            this.reqData.queryParams.queryData = JSON.stringify(option.queryData);
          }
          if (option.sortData) {
            this.reqData.queryParams.sortData = JSON.stringify(option.sortData);
          }
          if (option.focusId) {
            this.reqData.queryParams.focusId = option.focusId
          }
          if (option.groupData) {
            this.reqData.queryParams.groupData = JSON.stringify(option.groupData);
          }
          this.getList()
        }
      }
    }
  }
</script>
